import { Message, MessageBox } from 'element-ui'
import { local } from '@/utils/jcore/storage'
/**
 * instance: addSubmit editSubmit fromDataVali delSubmit updateList getByIdInfo
 * page component: addOpenDialog() delConfirm([rowList]) editOpenDialog(row) formDataSubmit()
 */
export default class DialogForm {
  constructor(_this, formData = {}, name) {
    this.data = _this
    this.name = name || ''
    this.onRepeat = false
    this.submitType = 0 // 提交状态 0 新增 1 编辑
    this.formData = JSON.stringify(formData)
    this.initValidData()
    // static data
    this.userInfo = local.get('authorityList')
    // tips
    this.$confirm = MessageBox.confirm
    this.$message = Message
    this.$alert = MessageBox.alert
  }
  // === reset start ===
  // 新增提交 - 1
  async addSubmit(data) {
    console.info('子类需重构 addSubmit')
  }
  // 编辑提交 - 2
  async editSubmit(row) {
    console.info('子类需重构 editSubmit')
  }
  // 表单验证 - 3
  async fromDataVali(data) {
    // 返回 true 验证成功 false 验证失败
    console.info('子类需重构 fromDataVali')
    return true
  }
  // 删除提交 - 4
  async delSubmit(rowList) {
    console.info('子类需重构 delSubmit')
  }
  // 更新列表 - 5
  async updateList() {
    console.info('子类需重构 updateList')
  }
  // 通过id获取详情 - 6
  async getByIdInfo(row) {
    console.info('子类需重构 getByIdInfo')
    return row
  }
  // === reset end ===
  initValidData() {
    if (this.name && this.data[this.name] === undefined) {
      return console.info(`未初始化 ${this.name}`)
    }
    if (this.getData('dialogVisible') === undefined) {
      console.info('未初始化 dialogVisible')
    }
    if (this.getData('addFormData') === undefined) {
      console.info('未初始化 addFormData')
    }
  }
  setData(data) {
    if (this.name) {
      Object.assign(this.data[this.name], data)
    } else {
      Object.assign(this.data, data)
    }
  }
  getData(key) {
    return this.name ? this.data[this.name][key] : this.data[key]
  }
  getDialogDom() {
    let refDom = `${this.name ? this.name + '_' : ''}dialogDom`
    let dom = this.data.$refs[refDom]
    if (!dom) {
      console.log(`未绑定弹窗 ref 为 ${refDom}`)
      return null
    }
    return dom
  }
  // 初始化表单
  initFormData() {
    let data = JSON.parse(this.formData)
    if (this.name) {
      this.data[this.name].addFormData = data
    } else {
      this.data.addFormData = data
    }
  }
  // 新增打开弹窗
  async addOpenDialog() {
    this.submitType = 0
    this.initFormData()
    this.setData({ dialogVisible: true })
    await this.data.$nextTick()
    let refDom = this.getDialogDom()
    if (refDom) {
      refDom.resetFields()
    }
  }
  // 表单提交
  async formDataSubmit() {
    let addFormData = this.getData('addFormData')
    let res = await this.fromDataVali(addFormData)
    if (res === true) {
      await this.dialogConfirm()
    }
  }
  // 新增/编辑确认
  async dialogConfirm() {
    if (this.onRepeat) return
    this.onRepeat = true
    try {
      let formData = this.getData('addFormData')
      if (this.submitType == 1) {
        await this.editSubmit(formData)
      } else {
        await this.addSubmit(formData)
      }
      setTimeout(() => {
        this.onRepeat = false
      }, 300)
      this.setData({ dialogVisible: false })
      await this.updateList()
    } catch (error) {
      console.error(error, 'dialogConfirm')
      this.onRepeat = false
    }
  }
  // 编辑打开弹窗
  async editOpenDialog(row) {
    let res = await this.getByIdInfo(row)
    await this.addOpenDialog()
    this.submitType = 1
    this.setData({ addFormData: res })
  }
  // 复制打开弹窗
  async copyOpenDialog(row) {
    let res = await this.getByIdInfo(row)
    res.name = ''
    delete res.id
    await this.addOpenDialog()
    this.submitType = 0
    this.setData({ addFormData: res })
  }
  // 删除确认操作
  async delConfirm(rowList) {
    await this.$confirm('确认删除吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    await this.delSubmit(rowList)
    await this.updateList()
  }
}
